package com.innovation.chengzhangriji.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.innovation.chengzhangriji.Baby;
import com.innovation.chengzhangriji.Config;
import com.innovation.chengzhangriji.R;
import com.innovation.chengzhangriji.database.DatabaseManager;
import com.innovation.chengzhangriji.ui.adapter.DiaryAdapter;
import com.innovation.chengzhangriji.ui.adapter.DiaryAdapter2;
import com.innovation.chengzhangriji.ui.widget.CuteToolbar;
import com.innovation.chengzhangriji.util.BabyUtil;
import com.innovation.chengzhangriji.util.ChZhRJUtils;

public class MyDiaryActivity extends BaseActivity implements DiaryAdapter2.DiaryAdapterAgent, CuteToolbar.OnClickCuteToolbarListener {

    private RecyclerView recyclerView;
    private DiaryAdapter2 diaryAdapter;
    private Baby[] babies = new Baby[0];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mydiary);

        // 初始化视图
        setupLayout();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setupData();
    }

    // 查找我创建的本地的宝宝列表
    private void setupData() {
        babies = DatabaseManager.getInstance(this).queryBabyAll(mLoginInfo.getUserID());
        recyclerView.setAdapter(diaryAdapter);
        diaryAdapter.notifyDataSetChanged();
    }

    // 初始化视图
    private void setupLayout() {
        CuteToolbar toolbar = (CuteToolbar) findViewById(R.id.toolbar);
        toolbar.setOnClickCuteToolbarListener(this);

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        diaryAdapter = new DiaryAdapter2(this);
    }

    @Override
    public String getBabyHeadUrl(int position) {
        return ChZhRJUtils.getText(babies[position].getImageUrl(), "");
    }

    @Override
    public String getDiaryName(int position) {
        return ChZhRJUtils.getText(babies[position].getNoteName(), babies[position].getName() + "的成长日记");
    }

    @Override
    public int getBabySex(int position) {
        return babies[position].getGender();
    }

    @Override
    public int getDiaryCount() {
        if(babies.length == 0) {
            return 0;
        }
        return babies.length + 2;
    }

    @Override
    public void OnItemClick(int position) {
        position -= 1;
        Intent intent = new Intent(this, BabyDiaryActivity.class);
        mLoginInfo.setBabyID(babies[position].getBabyID());
        Gson gson = new Gson();
        String loginValue = gson.toJson(mLoginInfo);
        Config.setLoginInfo(MyDiaryActivity.this, loginValue);
        startActivity(intent);
    }

    @Override
    public void onClickLeft() {
        finish();
    }

    @Override
    public void onClickRight() {

    }
}
